var nowIndex = 0;
var timeout = 4000;
var timer = null;
var imgNum = 4;
$(function(){
	
	timer = setInterval("slide()",timeout);
	
	//请求焦点图
	request("listIndexImg.do","pageIndex=0&pageSize=4&isNotPage=1",function(data){
		imgNum = data.length;
		for(var i = 0 ; i < data.length; i++){
			var img = data[i];
			var imgrow = $('<a target="_blank"><img src=""/></a>');
			var url = img['url'];
			if(url.indexOf("news") > -1 && url.indexOf("news.do") < 0){
				imgrow.attr("href","news.do?url="+encodeURI(proUrl(url)));
			}else{
				imgrow.attr("href",proUrl(url));
			}
			imgrow.find("img").attr("src",getImg(img['img']));
			var dotrow = $('<span></span>');
			if(i != 0){
				imgrow.find("img").addClass("hide");
			}else{
				dotrow.addClass("index-img-on");
			}
			$(".img-list").append(imgrow);
			$(".mask-dot").append(dotrow);
		}
		$(".index-imgs span").mouseenter(function(){
			clearInterval(timer);
			var s = $(this);
			$(".index-imgs span").removeClass("index-img-on");
			s.addClass("index-img-on");
			var index = s.index();
			$(".index-imgs img").hide();
			$(".index-imgs img").eq(index).show();
			
		})
		
		$(".index-imgs img").mouseenter(function(){
			clearInterval(timer);
		})
		
		$(".index-imgs img").mouseleave(function(){
			timer = setInterval("slide()",timeout);
		})
	});
	
	//热门新闻
	request("listNews.do","pageIndex=0&pageSize=6&isNotPage=1",function(data){
		var d = data.data;
		for(var i in d){
			var news = d[i];
			var row = $('<li><a target="_blank"></a></li>');
			row.find("a").text(news.title);
			row.find("a").attr("href","news.do?url="+encodeURI(proUrl(news.url)));
			$(".index-news ul").append(row);
		}
	});
	
	//热门图书
	request("listBook.do","pageIndex=0&pageSize=10&isNotPage=1&isHot=1",function(data){
		var d = data.data;
		for(var i in d){
			var row = $('<div class="index-book"><div class="book-content"><a target="_blank" class="book-img"><img/></a>'
            +'<div class="book-name"><a target="_blank"></a></div><div class="book-price"><span class="now-price"></span>'
            +'<span class="ori-price"></span></div></div></div>');
            var book = d[i];
            row.find(".book-img").attr("href","bookdetail.do?id="+book.id);
            row.find(".book-img img").attr("src",getImg(book['img']));
            row.find(".book-name a").text(book.name);
            row.find(".book-name a").attr("href","bookdetail.do?id="+book.id);
            row.find(".now-price").text("￥"+book.priceSelling);
            row.find(".ori-price").text("￥"+book.priceOriginal);
            $(".index-booklist").append(row);
		}
	});
	
	
})


function slide(){
	$(".index-imgs span").removeClass("index-img-on");
	$(".index-imgs span").eq(nowIndex).addClass("index-img-on");
	$(".index-imgs img").hide();
	$(".index-imgs img").eq(nowIndex).show();
	nowIndex = (++nowIndex) % imgNum;
}